package com.danskin.wechat.webapi;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.danskin.wechat.cache.JedisClient;
import com.danskin.wechat.entity.CustFeedback;
import com.danskin.wechat.entity.CustInfoEx;
import com.danskin.wechat.entity.WxMediaPlatform;
import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.service.CustFeedbackService;
import com.danskin.wechat.service.CustInfoExService;
import com.danskin.wechat.service.GiftService;
import com.danskin.wechat.util.DateUtils;
import com.danskin.wechat.util.JsonUtils;
import com.danskin.wechat.util.Tools;

@Controller
@RequestMapping("api/feedback")
public class CustFeedbackApiController extends BaseApiController {

	@Autowired
	private CustFeedbackService custFeedbackService;
	
	@Autowired
	private CustInfoExService custInfoService;

	@Autowired
	private GiftService giftService;

	@Autowired
	private JedisClient jedisClient;

	@Value("${GiftAccount}")
	private String GiftAccount;
	@Value("${GiftKeyActiveTime}")
	private Integer GiftKeyActiveTime;

	/**
	 * 测试链接 http://localhost:8080/danskin-wechat/api/feedback/saveFeedBack/
	 * wxd3150fe826ac186f 测试数据 { "openId": "o02XP1UXZxxshYsTpFGwIhfjCC2E",
	 * "name": "韩伟", "mobile": "1325689562", "replyBeginTime": "12",
	 * "replyEndTime": "18", "message": "fucking"}
	 * 
	 * @param map
	 * @param appId
	 * @return
	 */

	@RequestMapping(value = "/saveFeedBack/{appId}", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Object saveFeedBack(@RequestBody Map<String, Object> map, @PathVariable("appId") String appId) {
		AccessResponse<Object> access = new AccessResponse<Object>();
		if (map.containsKey("openId")) {
			if (!map.containsKey("name")) {
				access.setErrorCode("1");
				access.setErrorMsg("姓名不能为空");
				return JsonUtils.objectToJson(access);
			} else if (!map.containsKey("mobile")) {
				access.setErrorCode("1");
				access.setErrorMsg("手机号不能为空");
				return JsonUtils.objectToJson(access);
			}
			String openId = (String) map.get("openId");
			CustInfoEx custInfoEx = custInfoService.queryCustInfoExById(openId);
			if(StringUtils.isNotBlank(openId)&&null!=custInfoEx){
			map.remove("openId");
			CustFeedback bean = Tools.toBean(map, CustFeedback.class);
			String string = jedisClient.get(GiftAccount + ":" + openId);
			int account = -1;
			if (StringUtils.isNotBlank(string)) {
				account = Integer.parseInt(string);
			} else {
				account = 0;
			}
			if (0 == account) {
				jedisClient.set(GiftAccount + ":" + openId, "1");
				jedisClient.expire(GiftAccount + ":" + openId, DateUtils.getRemainSecondsInDay());
				int i = custFeedbackService.saveCustFeedbackSelective(bean, openId);
				if (i > 0) {
					// 调用感恩礼接口 接口开始
					AccessResponse<String> validate = giftService.validateGetActivityPrize(openId, 2);
					if (StringUtils.equals(validate.getErrorCode(), "-1")) {// 验证不通过,直接返回提示信息
						validate.setErrorCode("1");
						return JsonUtils.objectToJson(validate);
					} else {// 验证通过，执行领取奖品
						WxMediaPlatform mediaPlatform = this.getMPWithAccessToken(appId);
						if (mediaPlatform != null) {
							Object resultObject = giftService.getActivityPrize(openId, 2, mediaPlatform);
							if (resultObject == null) {
								access.setErrorCode("1");
								access.setErrorMsg("活动奖品领取失败");
							} else {
								access.setErrorCode("0");
								access.setErrorMsg("活动奖品领取成功");
								access.setData(resultObject);
							}
						} else {
							return this.getError("无效的AppId。");
						}
					}

				} else {
					access.setErrorCode("1");
					access.setErrorMsg("保存信息失败");
				}
			} else {
				access.setErrorCode("1");
				access.setErrorMsg("会员每天仅可以反馈一次");
				access.setData("");
			}
			}else{
				access.setErrorCode("1");
				access.setErrorMsg("openId不存在，请领取会员卡");
			}
		} else {
			access.setErrorCode("1");
			access.setErrorMsg("openId不能为空");
		}
		return JsonUtils.objectToJson(access);
	}

}
